<?php
require_once(APP_PATH . '/public/library/fckeditor/fckeditor.php');

class Clu_Form_Element_FckEditor extends Zend_Form_Element_Textarea
{

    /**@todo: use a custom decorator instead of overriding render.
     * You will have to add some kind of prefix path in order to be able to set the decorator property
    */
   
    /**
     * Render form element
     *
     * @param  Zend_View_Interface $view
     * @return string
     */
	protected $_fckOptions = array();

    public function render(Zend_View_Interface $view = null)
    {
        if (null !== $view) {
            $this->setView($view);
        }

        $oFCKeditor = new FCKeditor($this->getName());
        $oFCKeditor->BasePath = '/public/library/fckeditor/';
        $oFCKeditor->Config["CustomConfigurationsPath"] = $oFCKeditor->BasePath . "/hvn/config.js" . '?' . time() ; //TODO: remove the time()
        $oFCKeditor->Height = $this->_fckOptions['Height'];
        $oFCKeditor->Width = $this->_fckOptions['Width'];
        $oFCKeditor->Value        = $this->getValue();
        $oFCKeditor->ToolbarSet = $this->_fckOptions['ToolbarSet']; //TODO: remove "Source" button sometimes
        $oFCKeditor->Config['EnterMode'] = 'br';
        //$oFCKeditor->Config['EnterMode'] = '<p>';
        //$oFCKeditor->Config['DocType'] = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' ;

        // Editor Behaviour
        //$oFCKeditor->Config['FullPage'] = true ;
        //$oFCKeditor->Config['LinkDlgHideTarget'] = true ;
        //$oFCKeditor->Config['FullPage'] = true ;
        //$oFCKeditor->Config['StartupShowBlocks'] = true ;
		
        // Styles
        //$oFCKeditor->Config['EditorAreaStyles'] = 'body { color: Grey } h1 { color: Orange }';
        $oFCKeditor->Config['EditorAreaCSS'] = $oFCKeditor->BasePath .'/hvn/fck_editorarea.css' ;

        /*
        FCKConfig.RemoveFormatTags = 'b,big,code,del,dfn,em,font,i,ins,kbd,q,samp,small,span,strike,strong,sub,sup,tt,u,var' ;
        FCKConfig.IndentClasses = [ 'Indent1', 'Indent2', 'Indent3' ] ;
        $oFCKeditor->Config['CustomStyles'] = 
                {
                'Red Title' : { Element : 'h3', Styles : { 'color' : 'Red' } },
                'MY STYLE 1' : {Element :'h2', Styles : {'color' : 'Blue' , 'background-color' : 'Red' } }
                };
        */
        
        //HTML output
        //$oFCKeditor->Config['AdditionalNumericEntities'] = "'|>|<|\\+" ;
        $oFCKeditor->Config['TabSpaces'] = 4 ;
        
        //User Interface
        //FCKConfig.AutoDetectLanguage = true ;
		//FCKConfig.ContextMenu = ['Generic','Link','Anchor','Image','Flash','Select','Textarea','Checkbox','Radio','TextField', 'HiddenField','ImageButton','Button','BulletedList','NumberedList','Table','Form'] ;
		$oFCKeditor->Config['DefaultFontFormatLabel'] = 'Standard format' ;
		$oFCKeditor->Config['DisableFFTableHandles'] = true;
		$oFCKeditor->Config['DisableObjectResizing'] = true;
		$oFCKeditor->Config['EnableMoreFontColors'] = true ;
		$oFCKeditor->Config['FlashDlgHideAdvanced'] = false ;
		//$oFCKeditor->Config['ShowDropDialog'] = false ; $oFCKeditor->Config['ForcePasteAsPlainText'] = true;
		//$oFCKeditor->Config['SkinPath'] =  $oFCKeditor->BasePath + 'editor/skins/default';
		$oFCKeditor->Config['ToolbarLocation'] = 'In' ; //'None' 'Out:'
        $content = $oFCKeditor->CreateHtml();
        
        return $content;
    }   
    
    public function setOptions(array $options)
    {
        if (array_key_exists('fckOptions', $options)) {
        	$this->_fckOptions = $options['fckOptions'];
            unset($options['fckOptions']);
        }
        return parent::setOptions($options);
    }
    
            /*
        $this->removeDecorator('ViewHelper');
        foreach ($this->getDecorators() as $decorator) {
            $decorator->setElement($this);
            $content = $decorator->render($content);
        }
        */
        /*
<html>
<head>
<title>Test</title>
<script type="text/javascript" src="/code/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {

var whitelist = ['P', 'A'];

$('#click').click(function() {
	$($('textarea').val()).filter('*').each(function(i, o) {
		if ($.inArray(o.tagName, whitelist) === -1) {
			alert(o.tagName + " not allowed, removed!");
			$(o).remove();
		}
	});
});

});
</script>
</head>

<body>
<textarea cols="50" rows="10"><p>test</p><br><a href="link">linkname</a></textarea>
<input type="button" value="Click" id="click" />
<hr />
<pre>
<?php print htmlspecialchars(file_get_contents('test.php')); ?>
</pre>
</body>
</html>


* 
* */
}
?>